#include "Camera.h"
#include "../Core/Vector3.h"
#include "../Core/GlPlatform.h"
#include "../Core/Matrix4.h"

namespace View {
	using namespace ::Core;

	void Camera::setup() const {
		float t[4][4];
		Core::matrixTranspose( mViewMatrix, t );
		glLoadMatrixf( &t[0][0] );
	}

	void Camera::update() { 
		const auto& pv = mPosition.fromOrigin();

		mOrientation.toMatrix4( mViewMatrix );
		mViewMatrix[0][3] = -(right().dot(pv));
		mViewMatrix[1][3] = -(up().dot(pv));
		mViewMatrix[2][3] = -(forward().dot(pv));
	}
	
	const Vector3f Camera::vec(int idx) const {
		return Vector3f( mViewMatrix[idx][0], mViewMatrix[idx][1], mViewMatrix[idx][2] );
	}

	const Vector3f Camera::right() const {
		return vec(0);
	}

	const Vector3f Camera::up() const {
		return vec(1);
	}

	const Core::Vector3_t<float> Camera::forward() const {
		return vec(2);
	}
}